//
//  StudentsEnjoyableDetailViewController.m
//  ECBBusinessCamp
//
//  Created by rimi on 16/6/20.
//  Copyright © 2016年 邓毕华. All rights reserved.
//

#import "StudentsEnjoyableDetailViewController.h"

#import "TopicInformationView.h"
#import "InitiatorInfoView.h"
#import "JoinUserTableViewCell.h"
#import "JoinUserTableViewHeaderView.h"

#define TABLEVIEWIDENTIFIER @"joinUserTableViewCellIdentifier"

@interface StudentsEnjoyableDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, strong) TopicInformationView * topicInfomationView;  // 尊享主题信息视图
@property (nonatomic, strong) UIView * detailInfomationView; // 尊享信息视图
@property (nonatomic, strong) UIView * backgroundView; // 灰色背景
@property (nonatomic, strong) UILabel * detailInfomationLabel; // 尊享信息标签
@property (nonatomic, strong) UITableView * joinUserTableView; // 参与用户信息表格视图
@property (nonatomic, strong) JoinUserTableViewHeaderView * joinUserTableViewHeaderView; // 表格头部视图
@property (nonatomic, strong) InitiatorInfoView * initiatorInfoView; // 发起人信息视图
@property (nonatomic, strong) UIButton * backButton; // 返回按钮
@property (nonatomic, strong) NSDictionary * dataSource; // 数据源

@end

@implementation StudentsEnjoyableDetailViewController

#pragma mark 🌷🌷🌷life cycle🌷🌷🌷
- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    // 添加视图
    [self.view addSubview:self.topicInfomationView];
    [self.view addSubview:self.backgroundView];
    [self.view addSubview:self.backButton];
    
    [_backgroundView addSubview:self.detailInfomationView];
    [_detailInfomationView addSubview:self.detailInfomationLabel];
    
    // 自动布局
    [_topicInfomationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(SCREEN_WIDTH * 0.94);
        make.height.offset(SCREEN_HEIGHT * 0.3);
        make.centerX.equalTo(self.view);
        make.top.offset(SCREEN_HEIGHT * 0.08);
    }];
    [_backgroundView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(SCREEN_WIDTH);
        make.centerX.equalTo(self.view);
        make.top.equalTo(_topicInfomationView.mas_bottom);
        make.bottom.offset(SCREEN_HEIGHT * -0.09);
    }];
    [_detailInfomationView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.size.sizeOffset(CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT * 0.14));
        make.centerX.equalTo(_backgroundView);
        make.top.offset(0);
    }];
    [_backButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.height.offset(SCREEN_WIDTH * 0.065);
        make.left.offset(SCREEN_WIDTH * 0.06);
        make.bottom.offset(SCREEN_HEIGHT * -0.15);
    }];
    [_detailInfomationLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.offset(SCREEN_WIDTH * 0.9);
        make.centerX.equalTo(_detailInfomationView);
        make.top.offset(SCREEN_HEIGHT * 0.02);
    }];
}
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    // 网络请求
    [self loadData];
    
    _topicInfomationView.eid = _eid;
}

#pragma mark 🌷🌷🌷UITableViewDataSource🌷🌷🌷
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray * joinUserList = _dataSource[@"joinUserList"];
    return joinUserList.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    NSArray * joinUserList = _dataSource[@"joinUserList"];
    JoinUserTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:TABLEVIEWIDENTIFIER forIndexPath:indexPath];
    
    // 加载单元格数据
    cell.nameLabel.text = joinUserList[indexPath.row][@"username"];
    cell.phoneLabel.text = joinUserList[indexPath.row][@"phone"];
    cell.companyNameLabel.text = joinUserList[indexPath.row][@"company"];
    cell.positionLabel.text = joinUserList[indexPath.row][@"jog"];
    cell.addressLabel.text = joinUserList[indexPath.row][@"addr"];
    return cell;
}

#pragma mark 🌷🌷🌷UITableViewDelegate🌷🌷🌷
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.2 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        JoinUserTableViewCell * cell = [tableView cellForRowAtIndexPath:indexPath];
        cell.selected = NO;
    });
}

#pragma mark 🌷🌷🌷event responds🌷🌷🌷
- (void)respondsToBackBtn {
    [UIView animateWithDuration:0.5 animations:^{
        self.view.center = CGPointMake(SCREEN_WIDTH * 1.5, SCREEN_HEIGHT * 0.5);
    } completion:^(BOOL finished) {
        [self.view removeFromSuperview];
        [self removeFromParentViewController];
    }];
}

#pragma mark 🌷🌷🌷private methods🌷🌷🌷
- (void)loadData {
    NSMutableDictionary * paramters = [NSMutableDictionary dictionary];
    [paramters setObject:_eid forKey:@"eid"];
    [[NetTool sharedTool] requestMethod:POST loadingMsg:@"加载中..." url:BASEURLAPPEND(STUDENTSENJOYABLEDETAIL_URL) parameters:paramters finish:^(id responseObject, NSError *error) {
        if ([responseObject[@"status"] integerValue] == 1) {
            [MBProgressHUD showSuccess:@"加载完成！"];
            
            // 数据源赋值
            _dataSource = responseObject;
            
            // 请求数据成功 加载数据
            [self loadSuccess];
        }
    }];
}
- (void)loadSuccess {
    // 尊享主题信息视图数据加载
    NSDictionary * data = _dataSource[@"data"];
    [_topicInfomationView.leftImageView sd_setImageWithURL:[NSURL URLWithString:BASEURLAPPEND(data[@"pic"])] placeholderImage:[UIImage imageNamed:@"default_head.png"]];
    _topicInfomationView.themeLabel.text = [NSString stringWithFormat:@"尊享主题：%@", data[@"title"]];
    _topicInfomationView.originatorLabel.text = [NSString stringWithFormat:@"发起人：%@", data[@"fromuser"]];
    _topicInfomationView.deadlineLabel.text = [NSString stringWithFormat:@"截止日期：%@", [DateFormatter getDateSince1970BySeconds:[data[@"endtime"][@"time"] integerValue]]];
    _topicInfomationView.numberPeopleLabel.text = [NSString stringWithFormat:@"已有%@人参加", data[@"joinNum"]];
    _topicInfomationView.joinButton.userInteractionEnabled = [data[@"enjoyover"] isEqualToString:@"0"] ? YES : NO;
    _topicInfomationView.joinButton.hidden = [data[@"relations"] integerValue] == 0 || [data[@"relations"] integerValue] == 2 ? YES : NO;
    _topicInfomationView.overdueImageView.hidden = [data[@"relations"] integerValue] == 0 || [data[@"relations"] integerValue] == 2 || [data[@"enjoyover"] isEqualToString:@"0"] ? YES : NO;
    if ([data[@"relations"] integerValue] == 0 || [data[@"relations"] integerValue] == 2) {
        [_topicInfomationView.numberPeopleLabel mas_updateConstraints:^(MASConstraintMaker *make) {
            make.centerY.equalTo(_topicInfomationView.deadlineLabel);
        }];
    }
    
    // 尊享内容
    _detailInfomationLabel.text = [NSString stringWithFormat:@"尊享信息：\n%@", data[@"content"]];
    
    // 判断当前登录用户与发起人关系(其他班什么也不用做)
    if ([data[@"relations"] integerValue] == 0) {
        // 自己 加载参与用户信息表格视图
        [_backgroundView addSubview:self.joinUserTableViewHeaderView];
        [_backgroundView addSubview:self.joinUserTableView];
        
        [_joinUserTableViewHeaderView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.sizeOffset(CGSizeMake(SCREEN_WIDTH, 45));
            make.top.equalTo(_detailInfomationView.mas_bottom);
            make.centerX.equalTo(_backgroundView);
        }];
        [_joinUserTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(SCREEN_WIDTH);
            make.centerX.equalTo(_backgroundView);
            make.top.equalTo(_joinUserTableViewHeaderView.mas_bottom);
            make.bottom.offset(0);
        }];
        
        // 注册单元格
        [_joinUserTableView registerClass:[JoinUserTableViewCell self] forCellReuseIdentifier:TABLEVIEWIDENTIFIER];
    } else if([data[@"relations"] integerValue] == 1) {
        // 同班 加载发起人信息视图
        [_backgroundView addSubview:self.initiatorInfoView];
        
        // 自动布局
        [_initiatorInfoView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.width.offset(SCREEN_WIDTH * 0.9);
            make.centerX.equalTo(_backgroundView);
            make.top.equalTo(_detailInfomationView.mas_bottom).offset(SCREEN_HEIGHT * 0.06);
            make.bottom.offset(0);
        }];
    }
}

#pragma mark 🌷🌷🌷getters and setters🌷🌷🌷
- (TopicInformationView *)topicInfomationView {
    if (!_topicInfomationView) {
        _topicInfomationView = [[TopicInformationView alloc] init];
        [_topicInfomationView.joinButton setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
        _topicInfomationView.joinButton.layer.borderColor = [UIColor redColor].CGColor;
    }
    return _topicInfomationView;
}
- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc] init];
        _backgroundView.backgroundColor = RGB_COLOR(245, 245, 245, 1);
    }
    return _backgroundView;
}
- (UIView *)detailInfomationView {
    if (!_detailInfomationView) {
        _detailInfomationView = [[UIView alloc] init];
        // 添加底部的黑色线
        UIView * bottomView = [[UIView alloc] init];
        bottomView.backgroundColor = RGB_COLOR(132, 132, 132, 1);
        [_detailInfomationView addSubview:bottomView];
        
        // 自动布局
        [bottomView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.size.sizeOffset(CGSizeMake(SCREEN_WIDTH * 0.9, 1));
            make.centerX.equalTo(_detailInfomationView);
            make.bottom.offset(0);
        }];
    }
    return _detailInfomationView;
}
- (UILabel *)detailInfomationLabel {
    if (!_detailInfomationLabel) {
        _detailInfomationLabel = [[UILabel alloc] init];
        _detailInfomationLabel.numberOfLines = 0;
        _detailInfomationLabel.font = [UIFont systemFontOfSize:25];
        _detailInfomationLabel.textColor = RGB_COLOR(114, 114, 114, 1);
    }
    return _detailInfomationLabel;
}
- (UIButton *)backButton {
    if (!_backButton) {
        _backButton = [UIButton buttonWithType:UIButtonTypeCustom];
        [_backButton setImage:[UIImage imageNamed:@"btn_返回按钮.png"] forState:UIControlStateNormal];
        [_backButton addTarget:self action:@selector(respondsToBackBtn) forControlEvents:UIControlEventTouchUpInside];
    }
    return _backButton;
}
- (UITableView *)joinUserTableView {
    if (!_joinUserTableView) {
        _joinUserTableView = [[UITableView alloc] init];
        _joinUserTableView.backgroundColor = RGB_COLOR(245, 245, 245, 1);
        // 设置数据源
        _joinUserTableView.dataSource = self;
        // 设置代理
        _joinUserTableView.delegate = self;
        // 设置行高
        _joinUserTableView.rowHeight = 55;
        // 设置分隔线类型
        _joinUserTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _joinUserTableView;
}
- (InitiatorInfoView *)initiatorInfoView {
    if (!_initiatorInfoView) {
        _initiatorInfoView = [[InitiatorInfoView alloc] initWithInitiatorInfo:_dataSource[@"initiator"]];
    }
    return _initiatorInfoView;
}
- (JoinUserTableViewHeaderView *)joinUserTableViewHeaderView {
    if (!_joinUserTableViewHeaderView) {
        _joinUserTableViewHeaderView = [[JoinUserTableViewHeaderView alloc] init];
    }
    return _joinUserTableViewHeaderView;
}

@end
